package com.njlime.cms.service.app;

import com.njlime.cms.entities.Live;
import com.njlime.cms.entities.LiveComment;
import com.njlime.cms.repository.app.AppLiveDao;
import com.njlime.cms.sdk.weibo4j.json.JSONObject;
import com.njlime.core.helper.GlobalValue;
import com.njlime.core.helper.PageBean;
import com.njlime.member.entiies.MemberComment;
import com.njlime.member.service.app.AppMemberCollectionService;
import com.njlime.member.service.app.AppMemberCommentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AppLiveService {

    @Resource
    AppLiveDao appLiveDao;
    @Resource
    AppLiveCommentService appLiveCommentService;
    @Resource
    AppMemberCollectionService appMemberCollectionService;
    @Resource
    AppLiveUpService appLiveUpService;
    @Resource
    AppMemberCommentService appMemberCommentService;

    /**
     * 详情
     * @param id
     * @return
     */
    public Map<String, Object> get(int id) {
        return appLiveDao.get(id);
    }

    /**
     * 详情
     * @param id
     * @return
     */
    public Live findOne(int id) {
        return appLiveDao.findOne(id);
    }

    /**
     * 详情
     * @param id
     * @param uid
     * @return
     */
    public Map<String, Object> get(int id, Integer uid) {
        Map<String, Object> bean = appLiveDao.get(id);
        PageBean pb = new PageBean();
        LiveComment comment = new LiveComment();
        comment.setLive_id(id);
        pb = commentList(pb, comment, uid);
        bean.put("comment", PageBean.transformListMap(pb));
        //主播评论
        comment.setIs_admin(GlobalValue.YES);
        pb = new PageBean();
        pb.setPageSize(5);
        bean.put("actor_comment",commentList(pb, comment, null).getMapList());
        if (uid != null && uid > 0) {
            bean.put("collection", appMemberCollectionService.isCollected(uid, id, GlobalValue.BusinessType.LIVE));
        }
        return bean;
    }

    /**
     * 添加评论
     * @param liveComment
     */
    @Transactional
    public void addComment(LiveComment liveComment,MemberComment memberComment){
        appLiveCommentService.save(liveComment);
        if(memberComment != null){
            appMemberCommentService.save(memberComment);
            updateCommentMum(liveComment.getLive_id(), 1);
        }
    }

    public PageBean commentList(PageBean pageBean, LiveComment comment, Integer uid){
        pageBean = appLiveCommentService.list(pageBean,comment);
        if (pageBean.getMapList() != null && pageBean.getMapList().size() > 0) {
            List<Map<String, Object>> list = pageBean.getMapList();
            if(uid != null){
                for (Map<String, Object> map : list) {
                    Integer commentid = (Integer) map.get("id");
                    // 判断是否点赞过这条
                    if (commentid!= null && appLiveUpService.isUped(commentid, uid)) {
                        map.put("is_up", 1);
                    } else {
                        map.put("is_up", 0);
                    }
                }
            }
        }
        return pageBean;
    }

    /**
     * 列表
     * @param pageBean
     * @param tLive
     * @return
     */
    public PageBean list(PageBean pageBean, Live tLive) {
        return appLiveDao.list(pageBean, tLive);
    }

    /**
     * 评论数
     * @param live_id
     * @param num
     */
    @Transactional
    public void updateCommentMum(Integer live_id, int num) {
        appLiveDao.updateCommentMum(live_id, num);
    }

    /**
     * 更新观看人数
     * @param live_id
     */
    @Transactional
    public void updateViewCount(Integer live_id) {
        appLiveDao.updateViewCount(live_id);
    }
}
